package com.example.musicplayer.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.MusicActivity;
import com.example.musicplayer.bean.Album;

import java.io.Serializable;
import java.util.List;


public class SongSheetAdapter extends RecyclerView.Adapter<SongSheetAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final  int TYPE_CONTENT = 1;
    private List<Album> list;
    private ImageView mHeaderView;

    public SongSheetAdapter(List<Album> list) {
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return  new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_tab_listground,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.iv_item_songsheet_cover.setImageBitmap(list.get(position).getAlbumCover());
        holder.tv_item_songsheet_listname.setText(list.get(position).getName().toString());
        holder.tv_item_songsheet_number.setText(list.get(position).getNumber()+"首");
    }

    public ImageView getHeaderView() {
        return mHeaderView;
    }

    public void setData(List<Album> data) {
        list = data;
    }



    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_HEADER : TYPE_CONTENT;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tv_item_songsheet_listname;
        public ImageView iv_item_songsheet_cover;
        public TextView tv_item_songsheet_number;
        public ConstraintLayout cl_item_songsheet;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_item_songsheet_listname = (TextView) itemView.findViewById(R.id.tv_item_songsheet_listname);
            tv_item_songsheet_number = (TextView) itemView.findViewById(R.id.tv_item_songsheet_number);
            iv_item_songsheet_cover = (ImageView) itemView.findViewById(R.id.iv_item_songsheet_cover);
            cl_item_songsheet = (ConstraintLayout) itemView.findViewById(R.id.cl_item_songsheet);
            cl_item_songsheet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    System.out.println("position:"+position);
                    Intent intent = new Intent(itemView.getContext(), MusicActivity.class);
                    intent.putExtra("album",(Serializable)list.get(position));
                    intent.putExtra("isWEBAlbum",true);
                    itemView.getContext().startActivity(intent);
                }
            });
        }
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder{
        public ImageView headerImage;
        public HeaderViewHolder(View itemView) {
            super(itemView);
            headerImage = (ImageView) itemView.findViewById(R.id.header_image);
        }
    }
}
